class Test3_1 {
    public static void main(String[] args) {
        System.out.print("水仙花数（1~9999）：");
StringBuilder result = new StringBuilder();



        for (int num = 1; num <= 9999; num++) {
            if (isNarcissistic(num)) {
              if(result.length()>0){
result.append(",");
}
 result.append(num);
            }
        }
System.out.println(result.toString());
    }
public static boolean isNarcissistic(int num) {
        int originalNum = num;
        int sum = 0;
        int digits = String.valueOf(num).length();  // 获取数字位数
        
        while (num > 0) {
            int digit = num % 10;
            sum += Math.pow(digit, digits);  // 每位数字的n次方
            num /= 10;
        }
        
        return sum == originalNum;
    }
}
